﻿using Infrastructure.Log;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.FileFunction.Operation
{
    /// <summary>
    /// 操作文件系统
    /// </summary>
    public static class FilesUtils
    {

        private static readonly Logger _logger = new Logger(typeof(FilesUtils));

        /// <summary>
        /// 如不存在则创建目录
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static bool CreateDir(string dirPath)
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("FileUtils CreateDir()：" + ex);
                _logger.Error(ex.Message);
                return false;

            }
            return true;
        }

        /// <summary>
        /// 监测目论是否存在
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static bool DirExists(string dirPath)
        {
            return Directory.Exists(dirPath);
        }

        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static bool FileExistes(string filePath)
        {
            return File.Exists(filePath);
        }

        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">内容</param>
        /// <param name="DefualutEncoding">默认格式</param>
        public static void GenerateFile(string filePath, string content, string DefualutEncoding = "utf-8")
        {
            string s = filePath.Substring(0, filePath.LastIndexOf('\\'));

            CreateDir(s);


            Encoding encoding = Encoding.GetEncoding(DefualutEncoding);
            using (var fs = new FileStream(filePath, FileMode.Create))
            {
                StreamWriter sw = new StreamWriter(fs, encoding);
                //开始写入
                sw.Write(content);
                //清空缓冲区
                sw.Flush();
                //关闭流
            }
        }

        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="content">内容</param>
        /// <param name="DefualutEncoding">编码方式</param>
        public static void GenerateFile(string dirPath, string fileName, string content, string DefualutEncoding = "utf-8")
        {
            var filePath = $"{dirPath}\\{fileName}";
            GenerateFile(filePath, content, DefualutEncoding);
        }

        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="DefualutEncoding"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath, string DefualutEncoding = "utf-8")
        {
            if (!FileExistes(filePath))
            {
                throw new ArgumentNullException($"{filePath}文件不存在");
            }
            Encoding encoding = Encoding.GetEncoding(DefualutEncoding);
            return File.ReadAllText(filePath, encoding);
        }

        /// <summary>
        /// 读取文件夹中内容
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="filter">过滤器 "*.xml" "*.json"</param>
        /// <param name="searchOption">是否搜索子目录</param>
        /// <returns></returns>
        public static FileInfo[] GetFiles(string dirPath, string filter, SearchOption searchOption = SearchOption.TopDirectoryOnly)
        {
            if (!DirExists(dirPath))
            {
                throw new DirectoryNotFoundException($"{dirPath}不存在");
            }
            DirectoryInfo theFolder = new DirectoryInfo(dirPath);
            return theFolder.GetFiles(filter, searchOption);
        }

    }
}
